using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace ASPNETVN.PORTAL.Components.Security
{
    public partial class Role
    {
        public SqlDataReader GetRoleMembers(int roleID)
        {
            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"]);
            SqlCommand myCommand = new SqlCommand("Portal_GetRoleMembership", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleID;
            myCommand.Parameters.Add(parameterRoleID);

            // Open the database connection and execute the command
            myConnection.Open();
            SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            // Return the datareader
            return dr;
        }

        public int AssignRoleToUser(int roleID, int userID)
        {
            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"]);
            SqlCommand myCommand = new SqlCommand("p_Portal_Role_AssignRoleToUser", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter parameterReturnValue = new SqlParameter("@ReturnValue", SqlDbType.Int, 4);
            parameterReturnValue.Value = 0;
            parameterReturnValue.Direction = ParameterDirection.ReturnValue;
            myCommand.Parameters.Add(parameterReturnValue);

            // Add Parameters to SPROC
            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleID;
            myCommand.Parameters.Add(parameterRoleID);

            SqlParameter parameterUserID = new SqlParameter("@UserID", SqlDbType.Int, 4);
            parameterUserID.Value = userID;
            myCommand.Parameters.Add(parameterUserID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            return (int) parameterReturnValue.Value;
        }

        public void DeleteUserRole(int roleID, int userID)
        {
            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"]);
            SqlCommand myCommand = new SqlCommand("Portal_DeleteUserRole", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleID;
            myCommand.Parameters.Add(parameterRoleID);

            SqlParameter parameterUserID = new SqlParameter("@UserID", SqlDbType.Int, 4);
            parameterUserID.Value = userID;
            myCommand.Parameters.Add(parameterUserID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
}